﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using common;
using dao;
using dto;
using System.Collections;
using util;
using System.Reflection;

namespace GTVAccountingSystem.view.form
{
    public partial class frmMain : Form
    {
        public Type typeFunction;
        UsersDAO usersDAO = new UsersDAO();
        public ArrayList listOpen = new ArrayList();
        PopupManagerDAO popupManagerDAO = new PopupManagerDAO();
        public DataTable DTTMenuList;
        public MenuDAO menuDAO = new MenuDAO();

        public frmMain()
        {
            InitializeComponent(); 
        }
        public void getPopupUnread()
        {
            String msg = "";
            String actions = Constant.DR_ACTIONS_VARIABLE["varLockUser"].ToString();
            DataTable dt = popupManagerDAO.sp_getNewSystemLockUserByLoginFail(actions);
            int count = popupManagerDAO.getCountUnreadMessagesOfFeedBackAndForgetPassword(actions);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    msg += "System locked users\n";
                    msg += " + " + dt.Rows[i]["StaffCode"].ToString() + " - " + dt.Rows[i]["FirstName"].ToString() + "\n";
                }
            }
            if (count > 0)
            {
                msg += "You have " + count + " messages";
            }
            if (!msg.Equals(""))
            {
                MessageBox.Show(msg, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void createUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Enabled = false;
            /*Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmCreateNewUser"))
                {
                    frmCreateNewUser createUser = ((frmCreateNewUser)objs[1]);
                    createUser.Activate();
                    createUser.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }                
            }
            if(check)
            {
                    frmCreateNewUser createUser = new frmCreateNewUser(listOpen);
                    createUser.Show();
                    Object[] objs = { "frmCreateNewUser", createUser};
                    listOpen.Add(objs);
            }*/

        }

        private void updateUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmDetailUser"))
                {
                    frmDetailUser detailUser = ((frmDetailUser)objs[1]);
                    detailUser.Activate();
                    detailUser.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }
            }
            if (check)
            {
                //this.Enabled = false
                frmDetailUser detailUser = new frmDetailUser(this, listOpen.Count);
                //detailUser.MdiParent = this;
                detailUser.Show();
                Object[] objs = { "frmDetailUser", detailUser };
                listOpen.Add(objs);
            }
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmChangePassword"))
                {
                    frmChangePassword changePass = ((frmChangePassword)objs[1]);
                    changePass.Activate();
                    changePass.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }
            }
            if (check)
            {
                //this.Enabled = false;
                frmChangePassword changePass = new frmChangePassword(this, listOpen.Count);
                // changePass.MdiParent = this;
                changePass.Show();
                Object[] objs = { "frmChangePassword", changePass };
                listOpen.Add(objs);
            }
        }

        private void auditlogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //uclAuditlog auditLog = new uclAuditlog();
            //auditLog.Dock = DockStyle.Fill;
            //this.splitContainer1.Panel2.Controls.Add(auditLog);

            /*Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmAuditLog"))
                {
                    frmAuditlog auditLog = ((frmAuditlog)objs[1]);
                    auditLog.Activate();
                    auditLog.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }                
            }
            if (check)
            {
                //this.Enabled = false;
                frmAuditlog auditLog = new frmAuditlog(this, listOpen.Count);
                auditLog.MdiParent = this;
                auditLog.Show();
                Object[] objs = { "frmAuditLog", auditLog };
                listOpen.Add(objs);
            }*/
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            usersDAO.logoutByUserLoginID();
        }

        private void userinformationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            usercontrol.uclUserInformation userInformation = new usercontrol.uclUserInformation();
            this.splitContainer1.Panel2.Controls.Add(userInformation);
            userInformation.Dock = DockStyle.Fill;
            /*Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmUserInformation"))
                {
                    frmUserInformation userinformation = ((frmUserInformation)objs[1]);
                    userinformation.Activate();
                    userinformation.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }                
            }
            if (check)
            {
                //this.Enabled = false;90
                frmUserInformation userinformation = new frmUserInformation(this, listOpen.Count);
                userinformation.MdiParent = this;
                userinformation.Show();
                Object[] objs = { "frmUserInformation", userinformation };
                listOpen.Add(objs);
            }*/
        }

        private void feedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Boolean check = true;
            for (int i = 0; i < listOpen.Count; i++)
            {
                Object[] objs = (Object[])listOpen[i];
                if (objs[0].ToString().Equals("frmFeedback"))
                {
                    frmFeedback feedback = ((frmFeedback)objs[1]);
                    feedback.Activate();
                    feedback.WindowState = FormWindowState.Normal;
                    check = false;
                    break;
                }
            }
            if (check)
            {
                //this.Enabled = false;90
                frmFeedback feedback = new frmFeedback(this, listOpen.Count);
                feedback.Show();
                Object[] objs = { "frmFeedback", feedback };
                listOpen.Add(objs);
            }
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            getPopupUnread();
            //loadMenu(0);//call function load menu
        }
        /* Author : James
         * Date : 24/4/2012
         * function get the values menu and load
         * return menuStrip
         */

        /*
         * begin load menu
         */
        public void loadMenu(int depends)
        {
            try
            {
                DTTMenuList = menuDAO.getListMenu();
                if (DTTMenuList.Rows.Count > 1)
                {
                    foreach (DataRow dr in DTTMenuList.Select("depends='" + depends + "'", "id asc"))
                    {
                        CreateMenu(dr[0].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public void CreateMenu(string str)//create the subItem menu
        {
            ToolStripMenuItem menuitem = new ToolStripMenuItem(ReNameID(str));
            mnsMain.Items.Add(menuitem);
            if (DTTMenuList.Select("depends='" + str + "'").Length > 0)
            {
                foreach (DataRow dr in DTTMenuList.Select("depends='" + str + "'", "id asc"))
                {
                    CreateMenu(menuitem, dr[0].ToString());
                }
            }
        }
        public string CreateMenu(ToolStripMenuItem menuitem, string str)//create the item menu
        {
            if (DTTMenuList.Select("depends='" + str + "'").Length > 0)
            {
                menuitem.DropDownItems.Add(ReNameID(str), null, new EventHandler(menuitem_Click));
                foreach (DataRow dr in DTTMenuList.Select("depends='" + str + "'", "id asc"))
                {
                    CreateMenu(menuitem, dr[0].ToString());
                }
            }
            else
            {
                menuitem.DropDownItems.Add(ReNameID(str), null, new EventHandler(menuitem_Click));
                return str;
            }
            return str;
        }
        public string ReNameID(string str)
        {
            return DTTMenuList.Select("id='" + str + "'")[0][1].ToString();
        }
        public void menuitem_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            string link = DTTMenuList.Select("menuname='" + sender + "'", "id asc")[0]["link"].ToString();
            if (link != null)
            {
                string type = DTTMenuList.Select("menuname='" + sender + "'", "id asc")[0]["type"].ToString();
                if (type.Equals("frm"))
                {
                    typeFunction = Assembly.GetExecutingAssembly().GetType("GTVAccountingSystem.view.form." + link);
                    /*ConstructorInfo[] conInfo = typeFunction.GetConstructors();
                    for (int i = 0; i < conInfo.Length; i++)
                    {
                        ParameterInfo[] parInfo = conInfo[i].GetParameters();
                        for (int j = 0; j < parInfo.Length; j++)
                        {
                            if (parInfo[j].Name.Equals("main"))
                                list.Add(this);
                            else
                                list.Add(Constant.LIST_OPEN.Count);
                            Console.WriteLine("");
                        }
                    }*/
                    menuDAO.FunctionShowForm(typeFunction).ShowDialog();
                    Form frm = menuDAO.FunctionShowForm(typeFunction);
                    //typeFunction.GetConstructor(
                    frm.Show();
                }
                else if (type.Equals("ucl"))
                {
                    typeFunction = Assembly.GetExecutingAssembly().GetType("GTVAccountingSystem.view.usercontrol." + link);
                    this.splitContainer1.Panel2.Controls.Add(menuDAO.FunctionShowUserControl(typeFunction));
                    menuDAO.FunctionShowUserControl(typeFunction).Dock = DockStyle.Fill;
                }
            }
        }

        private void userInformationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (splitContainer1.Panel2.Controls.Count > 0)
                splitContainer1.Panel2.Controls.Clear();
            usercontrol.uclUserInformation uclInfo = new usercontrol.uclUserInformation();
            splitContainer1.Panel2.Controls.Add(uclInfo);
        }
        /*
         * end load menu
         */
    }
}
